package com.lhs.Controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.lhs.DTO.user.UserLoginDTO;
import com.lhs.DTO.user.UserRegisterDTO;
import com.lhs.Page.PageParam;
import com.lhs.Page.PageResult;
import com.lhs.Result.Result;
import com.lhs.service.UserService;
import com.lhs.VO.user.UserInfoVO;
import com.lhs.VO.user.UserLoginVO;
import com.lhs.entity.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("收到登录请求: {}", userLoginDTO);
        return userService.login(userLoginDTO);
    }

    @PostMapping("/isLogin")
    public Result isLogin() {
      Object id =  StpUtil.getLoginIdDefaultNull();

        return Result.success(StpUtil.isLogin()+"  id:"+id);
    }

    @PostMapping("/logout")
    public Result logout() {
        StpUtil.logout();
        return Result.success();
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {

        return userService.register( userRegisterDTO);
    }
    //获取登录中的用户信息
    @GetMapping("/getUserInfo")
    public Result<UserInfoVO> getUserInfo() {
        return userService.getUserById();
    }




    //修改自己的用户信息
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }

    //设置头像
    @PostMapping("/updateUserAvatar")
    public Result updateAvatar(@RequestPart("file") MultipartFile file) {

        return userService.updateAvatar(file);
    }

    //管理员接口


    //查询所有用户信息
    @GetMapping("/admin/getAllUserInfo")
    public Result<PageResult<User>> getAllUserInfo(@RequestBody(required = false) PageParam pageParam) {
        if (pageParam == null) {
            pageParam = new PageParam();
        }
        return userService.getAllUserInfo(pageParam);
    }

    //查询用户信息
    @GetMapping("/admin/getUserInfoById/{id}")
    public Result<UserInfoVO> getUserInfoById(@PathVariable("id") Long userId) {
        return userService.getUserInfoById(userId);
    }




    //修改用户信息

    @PostMapping("/admin/updateUserInfo/{userId}")
    public Result updateUserInfoById(@PathVariable("userId") Long userId,@RequestBody User user) {
        return userService.updateUserInfo_Admin(userId,user);
    }

   // 删除用户
@PostMapping("/admin/deleteUserById/{id}")
public Result<Boolean> deleteUserById(@PathVariable("id") Long userId) {
    // 参数校验
    if (userId == null || userId <= 0) {
        return Result.error("用户ID无效");
    }

    try {
        boolean result = userService.removeById(userId);
        if (result) {
            return Result.success("删除成功", true);
        } else {
            return Result.error("用户不存在，删除失败");
        }
    } catch (Exception e) {
        // 记录日志
        return Result.error("删除用户失败：" + e.getMessage());
    }
}


    // 批量查询用户信息
    @PostMapping("/admin/batch")
    public Result<Map<Long, UserInfoVO>> getUserInfoBatch(@RequestBody List<Long> userIds) {
        Map<Long, UserInfoVO> result = userService.getUserInfoBatch(userIds);
        return Result.success(result);
    }


}
